package com.dkm.biz.gl.domain;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dkm.base.api.annotion.SKGCacheable;
import com.dkm.base.api.constants.Bool;
import com.dkm.base.api.constants.RedisConstants;
import com.dkm.base.api.service.IdGenerator;
import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.core.util.string.StringUtils;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.biz.gl.persistence.dao.GlCateDao;
import com.dkm.biz.gl.persistence.entity.GlCatePo;
import com.dkm.biz.gl.repository.GlCateRepository;
import com.dkm.biz.gl.repository.GlTextRepository;

/**
 * 对象功能:总分类表。用于显示树层次结构的分类，可以允许任何层次结构。 领域对象实体
 * 开发公司:唯心六艺软件 - Craft6.cn
 * 开发人员:winston
 * 创建时间:2014-07-22 11:07:05
 */
@SuppressWarnings("serial")
@Service
@Scope("prototype")
public class GlCate extends AbstractDomain<String, GlCatePo>{
	 
	@Resource
	 private GlCateDao glCateDao = null;
	@Resource
	 private IdGenerator idGenerator;
	@Resource
	 private GlCateRepository glCateRepository;
	 
	 protected void init(){
		glCateDao = SpringHelper.getBean(GlCateDao.class);
		glCateRepository = SpringHelper.getBean(GlCateRepository.class);
		setDao(glCateDao);
	 }

	@Override
	public void save() {
		GlCate parent = null;
		GlCatePo glCatePo=getData();
		if(StringUtils.isNotEmpty(getData().getParentId())){
			parent = glCateRepository.load(getData().getParentId());
		}
		boolean isNew = isNew();
		if(isNew){
			//因为设置了id后，isNew()方法就失效了
			getData().setId(getIdGenerator().genSid());			
			getData().setStat(0);
		//	glCatePo.setKey(idGenerator.genSid());
		}		
		//设置树结构相关参数：深度、路径
		if(parent==null){						
			getData().setDepth(1);
			getData().setPath(getId());			
		}else{
			getData().setDepth(parent.getData().getDepth()+1);
			getData().setPath(parent.getData().getPath() + "." + getId());
		}		
		
		if(isNew){		
			create();
			/*GlTextPo glTextPo = new GlTextPo();
			glTextPo.setType(glCatePo.getTextKey());
			glTextPo.setContent(glCatePo.getTextContent());
			glTextPo.setEntityId(glCatePo.getId());
			glTextPo.setCreateTime(new Date());
			GlText glText =  glTextRepository.newInstance(glTextPo);
			glText.setNewFlag(true);
			glText.create();*/
		}else{
		/*	GlText glText = glTextRepository.load(glCatePo.getTextId());
			GlTextPo glTextPo = glText.getData();
			glTextPo.setType(glCatePo.getTextKey());
			glTextPo.setContent(glCatePo.getTextContent());
			glText.update();*/
			update();
		}	
	}	 
	 
	public void incrementStat(){
		if(getData().getStat()==null){
			getData().setStat(1);
		}else{
			getData().setStat(getData().getStat()+1);
		}
		save();
	}
	
	public void decreaseStat(){
		if(getData().getStat()!=null){
			getData().setStat(getData().getStat()-1);
		}
		save();
	}
		
	public List<GlCatePo> loadPath(){
		List<String> ids = getData().parsePath();
		List<GlCatePo> glCatePos = glCateRepository.findByIds(ids);
		return glCatePos;
	}
	
	@SKGCacheable(db=RedisConstants.SYSTEM,key="loadPathCache")
	public List<GlCatePo> loadPathCache(){
		List<String> ids = getData().parsePath();
		List<GlCatePo> glCatePos = glCateRepository.findByIds(ids);
		return glCatePos;
	}
	
	public void loadParentName(){
		if(StringUtils.isNotEmpty(getData().getParentId())){
			GlCate parent = glCateRepository.load(getData().getParentId());
			getData().setParentName(parent.getData().getName());
		}
	}
	//TODO 增加SKGCache，清除loadPachCache的数据
	public void updateStatWithParent(String id,Integer count){
		GlCate glCate = glCateRepository.load(id);
		if(glCate!=null){
			List<GlCatePo> glCatePos = glCate.loadPathCache();
			for(GlCatePo glCatePo:glCatePos){
				if(glCatePo.getStat()==null)glCatePo.setStat(0);
				glCatePo.setStat(glCatePo.getStat()+count);
				glCateDao.update(glCatePo);
			}	
		}
		
		
	}

	public boolean delete(String id) {
		List<GlCatePo> glCatePos =glCateRepository.findByParentId(id);
		if(glCatePos!=null && glCatePos.size()>0 ){
			return false;
		}
		destroy(id);
		return true;
	}
	
	/**
	 * 保存bi问题类型 
	 */
	public void saveServiceQuestion(GlCatePo glCatePo) {
		glCatePo.setIsShow(Bool.N.key()+"");
		String keyType = "biSeviceProble";
		glCatePo.setTypeKey(keyType);
		glCatePo.setSubName(glCatePo.getName());
		//判断是否为新增
		List<String> templeIds = new ArrayList<>();
		templeIds.add(glCatePo.getId());
		List<GlCatePo> templePos = glCateRepository.findByIds(templeIds);
		if(templePos != null && templePos.size() > 0){
		    glCateDao.update(glCatePo);
		}else {
			glCateDao.create(glCatePo);
		}
	}
	
	/**
	 *  删除问题类型及子类型
	 */
	public void deleteGlCateAndChildren(String glCateId) {
		List<String> templeIds = new ArrayList<>();
		templeIds.add(glCateId);
		List<GlCatePo> templePos = glCateRepository.findByIds(templeIds);
		if(templePos !=  null && templePos.size() > 0){
			   String typeKey = templePos.get(0).getTypeKey();
			  //删除子节点及自己
			   glCateDao.deleteChildrenByGlCate(glCateId,typeKey);
		}
	}
	
	/**
	 * 根据key查找除顶级分类、当前分类其它的分类
	 */
	public List<GlCatePo> findGlCateByKey(String glCateId) {
		List<String> ids = getData().parsePath();
		List<GlCatePo> glCatePos = glCateRepository.findByIds(ids);
		return glCatePos;
	}
	
}
